﻿using DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal.Statistics
{
    public partial class PrintQXFL : System.Web.UI.Page
    {
        private static string strStart { get; set; }
        private static string strEnd { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSearch_Click(sender, e);
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            strStart = txtStart.Text;
            strEnd = txtEnd.Text;
            string strDate = "";

            string strSql = "select row_number() over (order by JYLBDH) as SeqNum, JYLBDH,(select CON_CON from CONST_DEFINE where CON_CODE=table1.JYLBDH and CON_CHNAME='jylb') as jylb,(select COUNT(*) from (select distinct HPHM,HPZLDH from RESULT_VEHICLE_INFO where JYLBDH=table1.JYLBDH ";

            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSql += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
                lblDate.Text = DateTime.Parse(strStart).ToLongDateString().ToString() + " - " + DateTime.Parse(strEnd).ToLongDateString().ToString();
            }
            else
            {
                strDate = DbHelper.GetValue("select top 1 jcrq from RESULT_VEHICLE_INFO order by jcrq asc");
                lblDate.Text = DateTime.Parse(strDate).ToLongDateString().ToString() + " - " + DateTime.Now.ToLongDateString().ToString();
            }

            strSql += ") a) as CLZS from(select distinct(JYLBDH) from RESULT_VEHICLE_INFO where JYLBDH is not null) table1";

            DataTable m_dtTable = new DataTable();

            DbHelper.GetTable(strSql, ref m_dtTable);

            rptList.DataSource = m_dtTable;
            rptList.DataBind();

            m_dtTable.Dispose();
        }

        protected string getName(string jylbdh)
        {
            string[] jylb = jylbdh.Split(',');
            string name = "";
            string result = "";

            for (int i = 0; i < jylb.Length; i++)
            {
                if (!string.IsNullOrEmpty(jylb[i]))
                {
                    string strSql = "select con_con from CONST_DEFINE where con_chname='jylb' and con_code='" + jylb[i] + "'";
                    result = DbHelper.GetValue(strSql);
                    if (!string.IsNullOrEmpty(result))
                    {
                        name += result + "、";
                    }
                    else
                    {
                        name += jylb[i] + "、";
                    }
                }
            }
            return name.Substring(0, name.Length - 1);
        }
        protected string getHGS(string jylbdh)
        {
            string strSql = "";
            strSql = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSql += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSql += " GROUP BY HPHM,CLZLDH) RESULT";
            return DbHelper.GetValue(strSql);
        }

        protected string getHGL(string jylbdh)
        {
            float hgl = 0;
            string strSqlsb = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsb += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsb += " GROUP BY HPHM,CLZLDH) RESULT";
            string sb = DbHelper.GetValue(strSqlsb);

            string strSqlsp = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsp += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsp += " GROUP BY HPHM,CLZLDH) RESULT";
            string sp = DbHelper.GetValue(strSqlsp);
            if (int.Parse(sb) != 0)
            {
                hgl = (float.Parse(sp) / float.Parse(sb)) * 100;
            }
            return hgl.ToString("F2") == "0.00" ? "0" : hgl.ToString("F2");
        }

        protected string getBHGS(string jylbdh)
        {
            string strSql = "";
            strSql = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='2'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSql += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSql += " GROUP BY HPHM,CLZLDH) RESULT";
            return DbHelper.GetValue(strSql);
        }

        protected string getBHGL(string jylbdh)
        {
            float hgl = 0;
            string strSqlsb = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsb += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsb += " GROUP BY HPHM,CLZLDH) RESULT";
            string sb = DbHelper.GetValue(strSqlsb);

            string strSqlsp = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='2'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsp += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsp += " GROUP BY HPHM,CLZLDH) RESULT";
            string sp = DbHelper.GetValue(strSqlsp);
            if (int.Parse(sb) != 0)
            {
                hgl = (float.Parse(sp) / float.Parse(sb)) * 100;
            }
            return hgl.ToString("F2") == "0.00" ? "0" : hgl.ToString("F2");
        }

        protected string getYHGS(string jylbdh)
        {
            string strSql = "";
            strSql = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSql += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSql += " GROUP BY HPHM,CLZLDH) RESULT WHERE MAXLSH LIKE '%01'";
            return DbHelper.GetValue(strSql);
        }

        protected string getYHGL(string jylbdh)
        {
            float hgl = 0;
            string strSqlsb = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "'";

            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsb += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsb += " GROUP BY HPHM,CLZLDH) RESULT";

            string strSqlsp = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1' ";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsp += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsp += " GROUP BY HPHM,CLZLDH) RESULT WHERE MAXLSH LIKE '%01'";

            string sb = DbHelper.GetValue(strSqlsb);
            string sp = DbHelper.GetValue(strSqlsp);
            if (int.Parse(sb) != 0)
            {
                hgl = (float.Parse(sp) / float.Parse(sb)) * 100;
            }
            return hgl.ToString("F2") == "0.00" ? "0" : hgl.ToString("F2");
        }

        protected string getFHGS(string jylbdh)
        {
            string strSql = "";
            strSql = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSql += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSql += " GROUP BY HPHM,CLZLDH) RESULT WHERE MAXLSH NOT LIKE '%01'";
            return DbHelper.GetValue(strSql);
        }

        protected string getFHGL(string jylbdh)
        {
            float hgl = 0;
            string strSqlsb = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsb += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsb += " GROUP BY HPHM,CLZLDH) RESULT";

            string strSqlsp = "select count(*) from (SELECT HPHM,CLZLDH,MAX(JCLSH) as maxlsh FROM RESULT_VEHICLE_INFO WHERE JYLBDH='" + jylbdh + "' AND Z_PD='1'";
            if (strStart.Length > 0 && strEnd.Length > 0)
            {
                strSqlsp += " and jcrq between '" + strStart + "' and '" + strEnd + "'";
            }
            strSqlsp += " GROUP BY HPHM,CLZLDH) RESULT WHERE MAXLSH NOT LIKE '%01'";

            string sb = DbHelper.GetValue(strSqlsb);
            string sp = DbHelper.GetValue(strSqlsp);

            if (int.Parse(sb) != 0)
            {
                hgl = (float.Parse(sp) / float.Parse(sb)) * 100;
            }
            return hgl.ToString("F2") == "0.00" ? "0" : hgl.ToString("F2");
        }

        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            txtStart.Text = "";
            txtEnd.Text = "";
            btnSearch_Click(sender, e);
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {

        }
    }
}